import request from '@/utils/request';

/**
 * 分页查询资讯
 * @param params 查询条件
 */
export async function pageArticles(params) {
  const res = await request.get('/business/article/page', {
    params
  });
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 查询资讯列表
 * @param params 查询条件
 */
export async function listArticles(params) {
  const res = await request.get('/business/article', {
    params
  });
  if (res.data.code === 0 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 根据id查询资讯
 * @param id 资讯id
 */
export async function getArticle(id) {
  const res = await request.get('/business/article/' + id);
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加资讯
 * @param data 资讯信息
 */
export async function addArticle(data) {
  const res = await request.post('/business/article', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改资讯
 * @param data 资讯信息
 */
export async function updateArticle(data) {
  const res = await request.put('/business/article', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除资讯
 * @param id 资讯id
 */
export async function removeArticle(id) {
  const res = await request.delete('/business/article/' + id);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 批量删除资讯
 * @param data 资讯id集合
 */
export async function removeArticles(data) {
  const res = await request.delete('/business/article/batch', {
    data
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改资讯状态
 * @param ArticleId 资讯id
 * @param status 状态
 */
export async function updateArticleStatus(id, statusFlag) {
  const res = await request.put('/business/article/status', {
    id,
    statusFlag
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 推荐资讯
 * recommend
 */
export async function recommendArticle(data) {
  const res = await request.put('/business/article/recommend', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
